package action;

import Impl.adminDaoImpl;
import entity.Admin;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet(name = "AddAdmin")
public class AdminOpt extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("------------正在执行AdminOpt-------------");
        request.setCharacterEncoding("utf-8");
        String method = request.getParameter("method");
        String aname = request.getParameter("adminName");
        String apwd = request.getParameter("password");
        String phone = request.getParameter("phone");
        String email =request.getParameter("email");
        String role = request.getParameter("adminRole");
        Admin admin = new Admin(aname,apwd,phone,email,role);
        adminDaoImpl adminDao =new adminDaoImpl();
        if (method!=null && method.equals("edit")){
            int  id = Integer.parseInt(request.getParameter("id"));
            admin.setId(id);
            System.out.println("正在编辑管理员:"+aname);
            adminDao.updateAdmin(admin);
        }else if(method!=null && method.equals("del")){
            System.out.println("正在删除管理员:"+aname);
            int  id = Integer.parseInt(request.getParameter("id"));
            adminDao.deleteAdmin(id);
        }else{
            System.out.println("正在新增管理员:"+aname);
            adminDao.addNewAdmin(admin);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
